import shutil
import os
import os.path
import pdb
import sys
    
env = Environment()

env.Append(CCFLAGS = '-g')
env.Append(CCFLAGS = '-Wall')
env.Append(cc = 'gcc')
env.Append(CPPPATH = [''])
env.Append(LIBPATH = [''])
#env.Append(LIBS = ["pthread","cunit","rt","dl"])
Export('env')

package    = []
headfile   = []
dependlist = {}
initlist   = {}
configure  = {}

Export('headfile')
Export('dependlist')
Export('initlist')
Export('package')
Export('configure')

objs = []
obj = SConscript(Glob('src/*/sconscript'))
objs.append(obj)

package.sort()

for pkg in package:
    if '' == dependlist[pkg]:
        continue
    one_dependlist = dependlist[pkg]
    #pdb.set_trace()
    for one_dp in one_dependlist:
        if '' == one_dp:
            continue

        if one_dp == pkg:
            sys.exit('depend error 1!')
        
        if '' == dependlist[one_dp]:
            continue
        
        one_dplist = dependlist[one_dp]
        for temp in one_dplist:
            if '' == temp:
                continue
        
            if temp == pkg :
                print 'one pkg [%s] depend %s, \nbut pkg [%s] depend %s!'%(one_dp,one_dplist,pkg,one_dependlist)
                sys.exit('depend error 2!')
            
            flag = 0
            for old_dp in one_dependlist:
                if temp == old_dp:
                    flag = 1
                    break
            if not flag:
               one_dependlist.append(temp)

startlist = {}
i = 0
for pkg in package:
    startlist.update({pkg:i})
    i=i+1

for pkg in package:
    if '' == dependlist[pkg]:
        continue
    one_dependlist = dependlist[pkg]
    for one_dp in one_dependlist:
        if '' == one_dp:
            continue
        pkg_i = startlist[pkg]
        dp_i  = startlist[one_dp]
        if pkg_i < dp_i:
            del startlist[pkg]
            del startlist[one_dp]
            startlist.update({one_dp:pkg_i})
            startlist.update({pkg:dp_i})

startsort = {}
for pkg in package:
    i = startlist[pkg]
    startsort.update({i:pkg})
del startlist

#print startsort

#for pkg in package:
#    if '' == dependlist[pkg]:
#        continue
#    one_dependlist = dependlist[pkg]
#    print "%s"%(pkg)
#    print one_dependlist
#       

if os.path.exists('include'):
    for temp in Glob('include/*.h'):
        os.remove('%s'%(temp))
else:
    os.mkdir('include')

for temp in headfile:
    shutil.copy(str(temp),'include')

if not os.path.exists('lib'):
    os.mkdir('lib') 

fp = open('src/init.c','w')

length = len(startsort)
i = 0
while i < length:
    pkg = startsort[i]
    fp.write("extern void %s();\n"%(initlist[pkg][0]))
    fp.write("extern void %s();\n"%(initlist[pkg][1]))
    i = i + 1

fp.write("struct tagstartlist {\n    void * pinit;\n    void * pfini;\n    char * name;\n};\n")
fp.write("struct tagstartlist g_aStartList[] = {\n")

length = len(startsort)
i = 0
while i < length:
    pkg = startsort[i]
    fp.write("   {%s,%s,\"%s\"},\n"%(initlist[pkg][0],initlist[pkg][1],pkg))
    i = i + 1
fp.write("   {0,0,0}\n};\n")
fp.close()

objs.append("src/init.c")
env.Library('lib/dms', objs)


#make cfg for run.
fp_cfg = open('dms.cfg','w')
#pdb.set_trace()
for pkg in package:
    cfglist = configure[pkg]
    for temp in cfglist:
        fp_cfg.write("%s.%s=%s\n"%(pkg,temp[0],str(temp[1])))

fp_cfg.close()

#print package  
#print dependlist 
#print initlist
#print configure